<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-08-28 23:21
 *
 * 项目：rm  -  $  - UsersHelper.php
 *
 * 作者：liwei 
 */

namespace modules\levpays\table;

use Lev;
use lev\base\Modelv;
use lev\helpers\ModulesHelper;
use lev\helpers\UserHelper;
use modules\levmb\helpers\levmbSetHelper;
use modules\levmb\sdk\sdkHelper;
use modules\levmb\table\authuserHelper;

!defined('INLEV') && exit('Access Denied LEV');


class UsersHelper extends Modelv
{

    public static function tableName($tableName = '{{%lev_users}}', $prefix = true)
    {
        return parent::tableName($tableName ?: '{{%lev_users}}', $prefix); // TODO: Change the autogenerated stub
    }

    public static function getWxopenid($uid) {
        return ($r = static::findOne(['id'=> $uid])) ? $r['wxopenid'] : '';
    }

    public static function setWxopenid($uid, $openid) {
        if (!$openid) return;

        $r = static::findOne(['id'=> $uid]);
        if ($r) {
            static::update(['wxopenid'=>$openid], ['id'=>$uid]);
        }else if ($uid >0) {
            static::insert(['id'=>$uid, 'wxopenid'=>$openid, 'addtime'=>Lev::$app['timestamp']]);
        }
    }

    /**
     * 获取第一次支付成功时的UID
     * @param $openid
     * @param string $idField
     * @return mixed
     */
    public static function getFirstUid($openid, $idField = 'wxopenid') {
        $idField = $idField ?: 'wxopenid';
        return static::findOne("`$idField`='$openid' AND status=0 ORDER BY id DESC");
    }

    /**
     * 注册微信openid并返回对应UID
     * @param $openid
     * @param string $idField wxopenid alipayuserid baiduopenid
     * @return int
     */
    public static function tradeRegUid($openid, $idField = 'wxopenid')
    {
        if (!$openid) {
            return Lev::$app['uid'];
        }
        $idField = $idField ?: 'wxopenid';
        $u = static::findOne([$idField=>$openid]);
        if ($u) {
            return $u['id'];
        }
        if (ModulesHelper::isInstallModule('levmb')) {
            switch ($idField) {
                case 'alipayuserid' : $ckin = ['alipay']; break;
                case 'baiduopenid' : $ckin = ['bdumini']; break;
                default: $ckin = ['weixinmp']; break;
            }
            $authLogins = levmbSetHelper::authLoginx();
            foreach ($authLogins as $v) {
                in_array($v['loginType'], $ckin) && $logintypeids[] = $v['id'];
            }
            $where = " authuid='$openid' ";
            empty($logintypeids) || $where .= "AND logintypeid IN(".implode(',', $logintypeids).")";
            $u = authuserHelper::findOne($where);
            if ($u) {
                $uid = $u['uid'];
            }
        }
        if (empty($uid)) {
            if (Lev::$app['uid'] < 1) {
                $sortname = substr(Lev::$app['timestamp'], 1);
                $reg = UserHelper::onlyRegister('PA' . $sortname, mt_rand(100000, 999999), $sortname . '@pa.cn');
                if (empty($reg['uid'])) {
                    return 0;
                }
                $uid = $reg['uid'];
            } else {
                $uid = Lev::$app['uid'];
            }
        }
        if ($ck = static::findOne(['id'=>$uid])) {
            !$ck[$idField] && static::update([$idField=>$openid], ['id'=>$uid]);
        }else {
            static::insert(['id'=>$uid, $idField=>$openid, 'addtime'=>Lev::$app['timestamp']]);
        }
        return $uid;
    }
}